package com.example.videodemo;

import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dingfeng on 2016/7/17.
 */
public class VideoListFragment extends Fragment {

    ListView listview;

    List<VideoInfo> mVideoList = new ArrayList<>();

    public VideoListFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_video_list, container, false);
        listview = (ListView) view.findViewById(R.id.listview);
        mVideoList = GetVideoAsyncTask.getVideoList();
        VideoAdapter adapter = new VideoAdapter(getActivity(), mVideoList);
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                VideoInfo video = mVideoList.get(position);
                String path = video.mPath;
                Intent intent = new Intent();
                intent.setClass(getActivity(), VideoPlayActivity3.class);
                intent.setData(Uri.parse(path));
                intent.putExtra("video_title", video.mName);
                getActivity().startActivity(intent);
            }
        });
//        new GetLocalVideo().execute();
        return view;
    }

/*    class GetLocalVideo extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... params) {

            getLocalVideo();

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            VideoAdapter adapter = new VideoAdapter(getActivity(), videoList);
            listview.setAdapter(adapter);
        }
    }

    String proj[] = {
            MediaStore.Video.Media.DISPLAY_NAME,//视频的名字
            MediaStore.Video.Media.SIZE,//大小
            MediaStore.Video.Media.DURATION,//长度
            MediaStore.Video.Media.DATA,//播放地址
    };

    private void getLocalVideo() {
        ContentResolver cr = getContext().getContentResolver();
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {//有sd卡的情况
            Cursor cursor = cr.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);
            while (cursor.moveToNext()) {
                String name = cursor.getString(0);//得到视频的名字
                long size = cursor.getLong(1);//得到视频的大小
                long duration = cursor.getLong(2);//得到视频的时间长度
                String data = cursor.getString(3);//得到视频的路径，可以转化为uri进行视频播放

                VideoInfo videoInfo = new VideoInfo();
                videoInfo.setName(name);
                videoInfo.setPath(data);
                videoInfo.setDuration(duration);
                videoInfo.setSize(size);
                videoInfo.setThumb(data);

                videoList.add(videoInfo);
//                Log.d("dingfeng", "data:" + data);
            }
        }
        //不论是否有sd卡都要查询手机内存
        Cursor cursor = cr.query(MediaStore.Video.Media.INTERNAL_CONTENT_URI, proj, null, null, null);
        while (cursor.moveToNext()) {
            String name = cursor.getString(0);//得到视频的名字
            long size = cursor.getLong(1);//得到视频的大小
            long duration = cursor.getLong(2);//得到视频的时间长度
            String data = cursor.getString(3);//得到视频的路径，可以转化为uri进行视频播放

            VideoInfo videoInfo = new VideoInfo();
            videoInfo.setName(name);
            videoInfo.setPath(data);
            videoInfo.setDuration(duration);
            videoInfo.setSize(size);
            videoInfo.setThumb(data);

            videoList.add(videoInfo);
        }
    }*/
}
